package com.house.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.house.common.core.controller.BaseController;
import com.house.common.core.domain.AjaxResult;
import com.house.common.core.domain.entity.House;
import com.house.common.core.domain.entity.User;
import com.house.common.core.page.TableDataInfo;
import com.house.system.domain.HouseMedium;
import com.house.system.service.IHouseService;
import com.house.system.service.IUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.house.system.domain.Medium;
import com.house.system.service.IMediumService;

/**
 * 房屋中介表Controller
 *
 * @author tc
 * @date 2022-02-04
 */
@RestController
@RequestMapping("/domain/medium")
public class MediumController extends BaseController {
    @Autowired
    private IMediumService mediumService;

    @Autowired
    private IHouseService houseService;

    @Autowired
    private IUserService userService;


    /**
     * 查询房屋中介列表
     */
    @PreAuthorize("@ss.hasPermi('domain:medium:list')")
    @GetMapping("/list")
    public TableDataInfo list(Medium medium) {
        startPage();
        List<Medium> list = mediumService.selectMediumList(medium);
        return getDataTable(list);
    }

    /**
     * 查询房屋中介负责列表
     */
    @PreAuthorize("@ss.hasPermi('domain:medium:list')")
    @GetMapping("/list/{houseId}")
    public AjaxResult listAllot(@PathVariable("houseId") Long houseId) {
        AjaxResult ajax = AjaxResult.success();
        //查询房屋中介人列表
        List<Medium> mediumList = mediumService.selectMediumList(houseId, null);
        ajax.put("mediumList", mediumList);
        //查询房屋对象
        House house = houseService.selectHouseByHouseId(houseId);
        ajax.put("house", house);
        return ajax;
    }

    /**
     * 新增房屋中介表
     */
    @PreAuthorize("@ss.hasPermi('domain:medium:add')")
    @PostMapping
    public AjaxResult add(@RequestBody Medium medium) {
        AjaxResult ajax = AjaxResult.success();
        mediumService.insertMedium(medium);
        return ajax;
    }

    /**
     * 修改房屋中介表
     */
    @PreAuthorize("@ss.hasPermi('domain:medium:edit')")
    @PutMapping("/{userId}/{houseId}")
    public AjaxResult edit(@PathVariable("userId") Long userId, @PathVariable("houseId") Long houseId) {
        AjaxResult ajax = AjaxResult.success();
        Medium medium = new Medium();
        medium.setUserId(userId);
        medium.setHouseId(houseId);
        mediumService.updateMedium(medium);
        return ajax;
    }

    /**
     * 删除房屋中介表
     */
    @PreAuthorize("@ss.hasPermi('domain:medium:remove')")
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds) {
        return toAjax(mediumService.deleteMediumByUserIds(userIds));
    }

    /**
     * 查询房屋发布中介详情
     */
    @PreAuthorize("@ss.hasPermi('domain:medium:query')")
    @GetMapping("/{houseId}")
    public AjaxResult getMedium(@PathVariable("houseId") Long houseId) {
        AjaxResult ajax = AjaxResult.success();
        Medium medium = mediumService.selectMediumByHouseId(houseId);
        ajax.put("medium", medium);

        //查询房屋中介对象
        User user = userService.selectUserByUserId(medium.getUserId());
        ajax.put("user", user);
        return ajax;
    }
}
